package controller

import (
	"context"
	contract "develop_admin/contract/admin"
	products "develop_admin/contract/product"
	"develop_admin/contract/user"
	"develop_admin/model"
	"develop_admin/proto/admin"
	"develop_admin/utils"
	"github.com/ethereum/go-ethereum/common"
)

type Admin struct{}

//管理员登陆
func (a *Admin) AdminLogin(ctx context.Context, in *admin.AdminLoginRequest, out *admin.AdminLoginResponse) error {

	//生成实例
	newAdmin, err := contract.NewAdmin(common.HexToAddress("0xe439b2bc1495d0f2aa8c45431db54eb146568c92"), utils.Instance)
	if err != nil {
		return err
	}

	login, err := newAdmin.AdminLogin(utils.Instance.GetCallOpts(), in.Username, in.Password)
	if err != nil {
		return err
	}

	if login {
		out.Code = 200
		out.Message = "登陆成功"
	} else {
		out.Code = 500
		out.Message = "账号或密码不存在"
		return err
	}

	return nil
}

//展示所有用户
func (a *Admin) AllUser(ctx context.Context, in *admin.AllUserRequest, out *admin.AllUserResponse) error {
	//创建合约实例
	newUser, err := user.NewUser(common.HexToAddress("0x153ab45d8d1d649c2e63c745d446ac1cc649ba31"), utils.Instance)
	if err != nil {
		//0x153ab45d8d1d649c2e63c745d446ac1cc649ba31
		out.Code = 500
		out.Message = "获取用户失败"
		return err
	}

	//获取用户列表
	_, _, err = newUser.Get(utils.Instance.GetTransactOpts())
	if err != nil {
		out.Code = 500
		out.Message = "获取用户失败"
		return err
	}

	findUser, err := newUser.FindUser(utils.Instance.GetCallOpts())
	if err != nil {
		out.Code = 500
		out.Message = "获取用户失败"
		return err
	}
	var user []*admin.User //定义一个用户数组
	for i := 0; i < len(findUser); i++ {
		if findUser[i].Username != "" {
			var users admin.User
			users.UserName = findUser[i].Username
			s := findUser[i].Num.String()
			users.Num = s
			user = append(user, &users)
		}
	}
	out.Code = 200
	out.Message = "获取用户信息成功"
	out.User = user
	return nil
}

//删除用户
func (a *Admin) DeleteUser(ctx context.Context, in *admin.DeleteUserRequest, out *admin.AdminLoginResponse) error {
	newUser, err := user.NewUser(common.HexToAddress("0x153ab45d8d1d649c2e63c745d446ac1cc649ba31"), utils.Instance)
	if err != nil {
		out.Code = 500
		out.Message = "删除用户失败"
		return err
	}

	_, _, err = newUser.DeleteUser(utils.Instance.GetTransactOpts(), in.Name)
	if err != nil {
		out.Code = 500
		out.Message = "删除用户失败"
		return err
	}
	out.Code = 200
	out.Message = "删除用户成功"

	return nil
}

//展示所有订单
func (a *Admin) ShowAllOrder(ctx context.Context, in *admin.AllUserRequest, out *admin.ShowAllOrderResponse) error {
	product, err := products.NewProduct(common.HexToAddress("0x153ab45d8d1d649c2e63c745d446ac1cc649ba31"), utils.Instance)
	if err != nil {
		out.Code = 500
		out.Msg = "展示所有订单失败"
		return err
	}

	order, err := product.GetAllOrder(utils.Instance.GetCallOpts())
	if err != nil {
		out.Code = 500
		out.Msg = "展示所有订单失败"
		return err
	}
	var orders_rep []*admin.Order_Admin //存储所有订单
	for i := 0; i < len(order); i++ {
		var order_rep admin.Order_Admin
		order_rep.ProductName = order[i].OrderName
		order_rep.User = order[i].User
		order_rep.UserAddress = order[i].Address
		order_rep.Num = utils.StrToInt(order[i].OrderNum.String())
		order_rep.AllMoney = order[i].OrderPrice
		order_rep.Hash = order[i].Hash
		orders_rep = append(orders_rep, &order_rep)
	}

	out.Code = 200
	out.Msg = "展示所有订单成功"
	out.Order = orders_rep
	return nil
}

//展示所有反馈
func (a *Admin) ShowUserBacked(ctx context.Context, in *admin.ShowUserBackedRequest, out *admin.ShowUserBackedResponse) error {
	var info []model.FeedBack //存储所有反馈信息

	result := utils.Db.Find(&info)
	if result.Error != nil { //代表查询报错
		out.Code = 500
		out.Msg = "获取反馈信息失败"
		return result.Error
	}

	var infos_rep []*admin.Feedback
	for i := 0; i < len(info); i++ {
		var info_rep admin.Feedback
		info_rep.User = info[i].Email
		info_rep.GoodsInfo = info[i].FeedBackGoods
		info_rep.FeedbackInfo = info[i].FeedBackInfo
		infos_rep = append(infos_rep, &info_rep) //数据追加
	}

	out.Code = 200
	out.Msg = "获取反馈信息成功"
	out.Feedback = infos_rep

	return nil
}
